<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close" :append-to-body="true">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="风险性质" prop="riskproperty" :rules="rules.validateRequire">
            <el-select v-model="model.riskproperty">
              <el-option v-for="item in codeModel.riskPropertyList" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风险状态" prop="riskstatus">
            <el-select v-model="model.riskstatus" :disabled="true">
              <el-option v-for="item in codeModel.riskStatusList" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="风险描述" prop="riskdescription" :rules="rules.lengthRequire">
            <el-input v-model="model.riskdescription" type="textarea" :rows="3" placeholder="请输入对风险的详细描述" maxlength="1000" show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="风险可能性" prop="riskprobability" :rules="rules.validateRequire">
            <el-select v-model="model.riskprobability" placeholder="请选择风险可能性">
              <el-option v-for="item in codeModel.riskProbabilityList" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风险影响程度" prop="riskinfluencelevel" :rules="rules.validateRequire">
            <el-select v-model="model.riskinfluencelevel" placeholder="请选择风险影响程度">
              <el-option v-for="item in codeModel.riskInfluenceLevelList" :key="item.code" :label="item.name" :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="风险等级" prop="disprisklevel">
            <el-input v-model="model.disprisklevel" placeholder="根据影响程度和可能性计算" v-bind:class="classRiskLevel" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="风险影响评估" prop="influencequantization" :rules="rules.lengthRequire">
            <el-input v-model="model.influencequantization" placeholder="请输入对成本、进度、范围等的量化描述" maxlength="1000" show-word-limit type="textarea" :rows="3"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="风险应对计划" prop="riskmeasure" :rules="rules.validateRequire">
            <el-input v-model="model.riskmeasure" type="textarea" :rows="3" placeholder="请输入针对该风险的具体应对措施" maxlength="1000" show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="风险责任人" prop="responsible">
            <el-input v-model="model.responsible" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" :rules="rules.validateRequire">
          <el-form-item class="risk-date" label="计划完成日期" prop="riskmeasuredue">
            <el-date-picker v-model="model.riskmeasuredue" type="date" :picker-options="pickerOptions" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="description">
            <el-input v-model="model.description" type="textarea" maxlength="1000" show-word-limit :rows="3"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit" :disabled="disableSave"> <svg-icon icon-class="save" />保存 </el-button>
      <tl-attachment ref="attachmentRef" sourceType="MpmProjectRisk" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit">
        <el-button type="primary" :disabled="disableAttachment" icon="el-icon-paperclip" @click="$refs.attachmentRef.openUploadDialog()">{{ this.$refs.attachmentRef ? this.$refs.attachmentRef.btnTitle : '' }} [{{ this.$refs.attachmentRef ? (this.$refs.attachmentRef.fileList.length > 0 ? this.$refs.attachmentRef.fileList.length : this.$refs.attachmentRef.fileCount) : 0 }}] </el-button>
      </tl-attachment>
    </div>
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit_edit } from '../../../mixins/mixin_approval_edit'
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'

/** VUE配置 */
export default {
  mixins: [mixin_approval_edit_edit],
  components: {},
  data() {
    return {
      moduleTitle: '项目风险',
      urlPrefix: '/mpm/projectRisk/',
      url: {
        // 额外的url
        riskLevelMatrixUrl: '/mpm/projectRisk/riskLevelMatrix'
      },
      addRules: {
        // 输入框通用校验
        lengthRequire: [{ pattern: '.{1,}', required: 'true', message: '输入内容过短', trigger: 'change' }, { whitespace: true, required: 'true', message: '输入内容不能为空格', trigger: 'change' }]
      },
      // 风险评估矩阵
      riskLevelMatrix: '',
      disprisklevel: '',
      pickerOptions: {
        disabledDate: (time) => {
          let now = new Date()
          now.setHours(0, 0, 0, 0)
          return time.getTime() < now.getTime()
        }
      }
    }
  },
  // 传入projectinfo
  props: {
    projectinfo: {
      type: Object,
      default: () => {
        return {}
      }
    },
    render: {
      default: 'el-dialog',
      type: String
    }
  },
  computed: {
    // 当风险可能性和风险影响程度 任意一个更新后 拼串返回
    riskprobabilityAndriskinfluencelevel() {
      return `${this.model.riskprobability}|${this.model.riskinfluencelevel}`
    },
    classRiskLevel: function() {
      let res = ''
      switch (this.model.risklevel) {
        case 0:
          res = 'low-level'
          break
        case 1:
          res = 'middle-level'
          break
        case 2:
          res = 'high-level'
          break
      }
      return res
    }
  },
  watch: {
    // riskprobabilityAndriskinfluencelevel 更新后 将风险等级 risklevel，根据data()里的map 返回对应的 低中高 0 1 2
    riskprobabilityAndriskinfluencelevel(after, before) {
      if (after === 'null|null') {
        return
      }
      const [newProbability, newInfluenceLevel] = after.split('|')
      if (newProbability !== 'undefined' && newInfluenceLevel !== 'undefined') {
        this.$set(this.model, 'risklevel', this.riskLevelMatrix[newProbability][newInfluenceLevel])
        // 这个之后改成codemodellist获取
        let temp = this.codeModel.riskLevelList.filter((element) => {
          return parseInt(element.code) === this.model.risklevel
        })
        this.disprisklevel = temp[0].name
        this.$set(this.model, 'disprisklevel', temp[0].name)
      }
    }
  },
  methods: {
    assignDefaultValue() {
      if (this.isAdd) {
        if (this.codeModel.flowidList && this.codeModel.flowidList.length > 0) {
          this.$set(this.model, 'flowid', parseInt(this.codeModel.flowidList[0].id))
        }
        this.$set(this.model, 'orgcode', this.org.code)
        this.$set(this.model, 'disporgcode', this.org.name)
        this.$set(this.model, 'orgname', this.org.name)
        this.$set(this.model, 'deptcode', this.dept.code)
        this.$set(this.model, 'deptname', this.dept.fullname)
        this.$set(this.model, 'usercode', this.user.code)
        this.$set(this.model, 'username', this.user.name)
        this.$set(this.model, 'docdate', new Date())
        this.$set(this.model, 'submitdate', new Date())
        this.$set(this.model, 'state', 0)
        this.$set(this.model, 'riskdate', new Date())
        this.$set(this.model, 'riskstatus', '0')
        this.$set(this.model, 'projectid', this.projectinfo.id)
        this.$set(this.model, 'projectlevel', this.projectinfo.projectlevel)
        this.$set(this.model, 'dispprojectlevel', this.projectinfo.dispprojectlevel)
        this.$set(this.model, 'projectname', this.projectinfo.projectname)
        this.$set(this.model, 'projectcode', this.projectinfo.projectcode)
        this.$set(this.model, 'projectmanager', this.projectinfo.projectmanager)
        this.$set(this.model, 'projectmanagercode', this.projectinfo.projectmanagercode)
        this.$set(this.model, 'responsible', this.projectinfo.projectmanager)
        this.$set(this.model, 'responsiblecode', this.projectinfo.projectmanagercode)
      }
    },
    /** 抽屉组件必有 **/
    doCancelEdit() {
      this.visible = false
      this.$emit('close')
      this.$emit('refresh')
    },
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
    },
    // 获取风险等级矩阵
    getRiskLevelMatrix() {
      getAction(this.url.riskLevelMatrixUrl, '').then((res) => {
        if (res.success) {
          let matrix = JSON.parse(res.result.riskLevelMatrix)
          this.$set(this, 'riskLevelMatrix', matrix)
        }
      })
    }
  },
  created() {
    this.getRiskLevelMatrix()
  }
}
</script>
<style lang="scss" scoped>
.risk-date /deep/ .el-form-item__label {
  padding: 0 6px 0 0;
}

.tl-drawer /deep/ .el-drawer__body {
  overflow: unset;
}

$riskLevels: high, middle, low;
$riskColors: #ff6363, #ffe119, #bfef45;
//for 循环 根据风险低中高 匹配颜色和字体样式
@for $i from 1 through 3 {
  .#{nth($riskLevels,$i)}-level /deep/ .el-input__inner {
    background-color: nth($riskColors, $i);
    //背景红色 则加粗
    @if $i == 1 {
      font-weight: bold;
    }
    color: #303133 !important;
  }
}
</style>
